package customer

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/app/server"
	serviceCustomer "sieve_admin_server_userpass/app/service/customer"
)

var (
	_              util.API = (*SieveConfigAPI)(nil)
	SieveConfigSet          = wire.NewSet(wire.Struct(new(SieveConfigAPI), "*"))
)

type SieveConfigAPI struct {
	util.EmptyAPI
	SieveConfigSrv *serviceCustomer.SieveConfigSrv
}

func (a *SieveConfigAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "sieveConfig",
			Title: "筛源配置", Perms: []*server.Perm{
				{Name: "add", Title: "新增"},
				{Name: "query", Title: "查询"},
				{Name: "edit", Title: "修改"},
				{Name: "del", Title: "删除"},
			},
			Order: 3,
		},
	}
}

func (a *SieveConfigAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("name"),
				contextx.NewVar("label"),
				contextx.NewVar("alias"),
				contextx.NewVar("default", true),
			},
		)
	)

	return a.SieveConfigSrv.Create(context.Background(), reqArgs)
}

func (a *SieveConfigAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("name"),
				contextx.NewVar("default", true),
			},
		)

		p       = schema.PaginationParam{}
		repKeys = []string{"*"}
	)

	return a.SieveConfigSrv.Query(context.Background(), reqArgs, p, repKeys)
}

func (a *SieveConfigAPI) API_edit_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("name"),
				contextx.NewVar("alias"),
				contextx.NewVar("status", true),
				contextx.NewVar("default", true),
			},
		)
	)

	return a.SieveConfigSrv.Update(context.Background(), uint64(ctx.Uid), reqArgs)
}

func (a *SieveConfigAPI) API_del_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.SieveConfigSrv.Delete(context.Background(), reqArgs)
}
